//P44 45
//找x或x的后继
int bin_search(int *a,int n,int x){//a[0]到a[n-1]单调递增
    int left=0,right=n;
    while(left<right){
        int mid=left+(right-left)/2;
        if(a[mid]>=x)right=mid;
        else left=mid+1;
    }
    return left;
}

//找x或x的前驱
int bin_search2(int *a,int n,int x){
	int left=0,right=0;
	while(left<right){
		int mid=left+(right-left+1)/2;
		if(a[mid]<=x)left=mid;
		else right=mid-1;
	}
}